package com.awesome.activitydemo.activities;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.awesome.activitydemo.App;
import com.awesome.activitydemo.R;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import me.leolin.shortcutbadger.ShortcutBadger;

/**
 * 角标的页面
 * https://app.yinxiang.com/shard/s35/nl/26047886/0d32a141-dde6-4e36-8707-35da5b09a3a0
 */
public class BadgeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_badge);

        findViewById(R.id.btn_show_message).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setBadgeNumForHonor(1);
            }
        });
        findViewById(R.id.btn_change_message).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setBadgeNumForEverything(2);
            }
        });
        findViewById(R.id.btn_hide_message).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeBadgeNumForEverything();
            }
        });
    }

    /**
     * 小米系统设置角标
     * @param num
     */
    private void setBadgeNumForXiaomi(int num) {
        NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this).setContentTitle("title")
                .setContentText("text").setSmallIcon(R.drawable.ic_launcher_background);
        Notification notification = builder.build();
        try {
            Field field = notification.getClass().getDeclaredField("extraNotification");
            Object extraNotification = field.get(notification);
            Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);
            method.invoke(extraNotification, num);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mNotificationManager.notify(0, notification);
    }

    public void setBadgeNumForHuawei(int num) {
        Bundle extra = new Bundle();
        extra.putString("package", "com.awesome.activitydemo");
        extra.putString("class", "com.awesome.activitydemo.MainActivity");
        extra.putInt("badgenumber",num);
        App.Companion.getInstance().getApplicationContext().getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, extra);
    }

    /**
     * 荣耀系统设置角标
     * @param num
     */
    public void setBadgeNumForHonor(int num) {
        String URI_OLD = "content://com.huawei.android.launcher.settings/badge/";
        String URI_NEW = "content://com.hihonor.android.launcher.settings/badge/";
        Uri uri = Uri.parse(URI_NEW);
        String type = getContentResolver().getType(uri);
        if (TextUtils.isEmpty(type)) {
            uri = Uri.parse(URI_OLD);
            type = getContentResolver().getType(uri);
            if (TextUtils.isEmpty(type)) {
                uri = null;
            }
        }
        try {
            Bundle extra = new Bundle();
            extra.putString("package", "com.awesome.activitydemo");
            extra.putString("class", "com.awesome.activitydemo.MainActivity");
            extra.putInt("badgenumber", num);
            if (uri != null) {
                getContentResolver().call(uri, "change_badge", null, extra);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 任何系统设置角标
     * @param num
     */
    public void setBadgeNumForEverything(int num) {
        boolean success = ShortcutBadger.applyCount(App.Companion.getInstance().getApplicationContext(), 1);
        Toast.makeText(getApplicationContext(), "Set count=" + 1 + ", success=" + success, Toast.LENGTH_SHORT).show();
    }

    /**
     * 任何系统清除角标
     */
    public void removeBadgeNumForEverything() {
        ShortcutBadger.removeCount(App.Companion.getInstance().getApplicationContext());
    }
}